<?php

namespace Home\Controller;

use Think\Controller;
use \Model\StuIdentModel;
use \Model\SkillModel;
use Common\Common\UserGlobal;
use Model\UserModel;  //公共类

class StuIdentController extends CommonController
{

	// 用户学生认证
	public function student_identification()
	{

		$user_id = $_POST['user_id'];
		$stu_school = $_POST['stu_school'];
		$stu_enrollment = $_POST['stu_enrollment'];
		$stu_academic_year = $_POST['stu_academic_year'];
		$stu_number = $_POST['stu_number'];
		$stu_ident_img = $_FILES['stu_ident_img'];
		// dump($stu_ident_img);


		$stuident = new StuIdentModel();

		//检测用户认证状态，若已认证直接提示“已认证”并结束
		// echo $user_id;exit;
		$stuident->model_check_user_stu_state($user_id); 
		exit;
	   	//收集表单信息
		if ($_FILES['stu_ident_img']['error'] === 0) {
			// echo "1";
			$cfg = array(
				'rootPath' => './Public/Upload/Student/', //保存根路径
				'savePath'   =>  '',
				'saveName'   =>  'time',
				'subName'    =>  $user_id

			);
				//设置附件的存储位置
			$up = new \Think\Upload($cfg);
				//上传附件
				//如果附件上传成功，就可以通过uploadOne的返回值查看到附件在服务器的存储情况
			$z = $up->uploadOne($_FILES['stu_ident_img']);

			//附件保存到数据库中,保存路径名即可
			$stu_ident_img = $z['savename'];
			$_POST['stu_ident_img'] = $stu_ident_img;
			$_POST['stu_applyfor_time'] = date('y-m-d H:i:s', time());

		}
		
		// 如果创建失败 表示验证没有通过 输出错误提示信息
		if (!$data = $stuident->create()){
			$err_info = $stuident->getError();
			echo '{"msg":"'.$err_info.'"}';
			exit;
		}

		$z = $stuident->add($data);
		if ($z) {
			$usermodel = new UserModel();
			$info = $usermodel->model_update_user_student_state($user_id,1);
			if($info) {
				echo '{"msg":"ok"}';return;	//一切ok，进入待审核状态
			}else{
				echo '{"msg":"操作失败!"}';return;	//数据库操作不成功
			}
		} else {
			echo '{"msg":"图片上传失败！"}';return;
		}
	}




}